
package pl.genapp.stocksense;

import java.util.Calendar;
import java.util.Date;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.TimeChart;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import pl.genapp.appsense.R;
import pl.genapp.stocksense.others.TimeChartHelper;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;

public class EntityScreenSentGraphs {
	
	private EntityScreenFragment parent;
	private View layout;
	private LinearLayout[] sentGraphPlaces;
	private View emptyInfo;
	private Boolean graphsVisible = false;
	private Animation rotation;
	
	public EntityScreenSentGraphs(EntityScreenFragment parent) {
		this.parent = parent;
	}
	
	View setupView() {
		
		if (parent.getActivity() != null) {
			
			layout = parent.getActivity().getLayoutInflater().inflate(R.layout.fragment_entity_screen_three, null);
			
			// polaczenie z UI
			sentGraphPlaces = new LinearLayout[] { (LinearLayout) layout.findViewById(R.id.sentGraph1), (LinearLayout) layout.findViewById(R.id.sentGraph2),
					(LinearLayout) layout.findViewById(R.id.sentGraph3), (LinearLayout) layout.findViewById(R.id.sentGraph4), (LinearLayout) layout.findViewById(R.id.sentGraph5),
					(LinearLayout) layout.findViewById(R.id.sentGraph6) };
			
			emptyInfo = layout.findViewById(R.id.emptyInfoImageSentGraphs);
			rotation = AnimationUtils.loadAnimation(parent.getActivity(), R.anim.clockwise_rotation);
			// emptyInfo.startAnimation(rotation);
			
			return layout;
			
		}
		return null;
		
	}
	
	void clearGraphs() {
		
		for (LinearLayout v : sentGraphPlaces) {
			v.removeAllViews();
		}
		emptyInfo.setVisibility(View.VISIBLE);
		// emptyInfo.startAnimation(rotation);
		graphsVisible = false;
		
	}
	
	void doGraphsThings() {
		
		emptyInfo.setVisibility(View.GONE);
		// emptyInfo.clearAnimation();
		graphsVisible = true;
		
		final Calendar startDate = Calendar.getInstance();
		int numberOfDays = parent.getSentimentsChartsData()[0].size();
		String[] types = { "CALM", "ALERT", "SURE", "VITAL", "KIND", "HAPPY" };
		int k = 0;
		
		startDate.add(Calendar.DAY_OF_MONTH, (-1) * numberOfDays);
		
		for (LinearLayout v : sentGraphPlaces) {
			
			XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
			XYMultipleSeriesRenderer mRenderer = TimeChartHelper.initializeRendered(types[k]);
			XYSeriesRenderer r = TimeChartHelper.initializeFinanceLine();
			mRenderer.addSeriesRenderer(r);
			
			TimeSeries series = new TimeSeries("test");
			
			long valueTime = startDate.getTime().getTime();
			
			for (int i = 0; i < numberOfDays; i++) {
				series.add(new Date(valueTime + i * TimeChart.DAY), (Double) parent.getSentimentsChartsData()[k].get(i));
			}
			
			mDataset.addSeries(series);
			
			if (parent.getActivity() != null) {
				GraphicalView mChartView = ChartFactory.getTimeChartView(parent.getActivity(), mDataset, mRenderer, "dd/MM");
				
				v.removeAllViews();
				v.addView(mChartView);
				k++;
			}
			
		}
		
	}
}